﻿using System;
using System.Text;
using AnjLab.SyncIT.Core.Api;
using AnjLab.SyncIT.Core.Data.Outlook;

namespace AnjLab.SyncIT.Core.Data.GoogleCode
{
    public class Issue : ISourceTask
    {
        public string ID { get; set; }

        public string Summary { get; set; }
        public string Status { get; set; }
        public string Owner { get; set; }
        public string CC { get; set; }

        public string Opened { get; set; }
        public string Closed { get; set; }
        public string Modified { get; set; }

        public string StartDate { get; set; }
        public string EndDate { get; set; }

        public Importance Importance
        {
            get
            {
                foreach (var label in Labels)
                {
                    if (label.StartsWith("Priority"))
                    {
                        string priority = label.Split('-')[1].ToLower();

                        switch (priority)
                        {
                            case "critical":
                            case "high":
                                return Api.Importance.High;

                            case "low":
                                return Api.Importance.Low;
                        }

                        return Api.Importance.Normal;
                    }
                }

                //  No priority were specified
                return Api.Importance.Normal;
            }
        }

        public int Stars { get; set; }

        public string Reporter { get; set; }
        public string Description { get; set; }

        /// <summary>
        /// Labels provide detailed information on issues: "Project", "Component", "Priority", etc.
        /// </summary>
        public string[] Labels { get; set; }

        public Attachment[] Attachments;

        public Comment[] Comments { get; set; }

        public string LabelsAsText
        {
            get
            {
                var builder = new StringBuilder();
                bool firstLabel = true;
                foreach (string label in Labels)
                {
                    if (!firstLabel)
                    {
                        builder.Append(", ");
                    }
                    builder.Append(label);
                    firstLabel = false;
                }
                return builder.ToString();
            }
        }

        public string AttachmentsAsText
        {
            get { return Comment.AttachmentsAsString(Attachments); }
        }

        public bool IsClosed
        {
            /*
             * See http://code.google.com/p/outlooktrac/feeds/issueOptions
             * about issue's Close status
             * If the issue is closed then the date issue were closed is the 
             * date of the last comment
             */

            get {
                switch (Status)
                {
                    case "Fixed":
                    case "Verified":
                    case "Invalid":
                    case "Duplicate":
                    case "WontFix":
                        return true;
                }
                return false;
            }
        }

        public bool IsAssignedToMe(Source source)
        {
            return string.Equals(Owner, source.UserName, StringComparison.InvariantCultureIgnoreCase);
        }

        public void InitializeOutlookTask(IOutlookItem outlookTask)
        {
            
        }

        public void UpdateOutlookTask(IOutlookItem outlookTask)
        {
            
        }

        public override string ToString()
        {
            return string.Format("Issue {0}: {1}; Owner = {6}; Status: {5}; Opened: {4}; Modified: {3}; {2} Comments",
                                 ID, Summary, Comments == null ? "?" : Comments.Length.ToString(), Modified, Opened,
                                 Status, Owner);
        }

        public class Comment
        {
            public readonly string ID;
            public readonly string Author;
            public readonly string Timestamp;
            public readonly string Message;

            public Attachment[] Attachments { get; set; }

            public Comment(string ID, string author, string timestamp, string message)
            {
                this.ID = ID;
                Author = author;
                Timestamp = timestamp;
                Message = message;
            }

            public override string ToString()
            {
                var builder = new StringBuilder();
                builder.Append("Comment #");
                builder.Append(ID);
                builder.Append(" by ");
                builder.Append(Author);
                builder.Append("\nPublished ");
                builder.Append(Timestamp);
                builder.Append("\n\n");
                builder.Append(Message);
                builder.Append(AttachmentsAsString(Attachments));
                return builder.ToString();
            }

            public static string AttachmentsAsString(Attachment[] attachments)
            {
                var builder = new StringBuilder();
                if (attachments != null && attachments.Length > 0)
                {
                    builder.Append("\n");
                    builder.Append(attachments.Length);
                    if (attachments.Length == 1)
                    {
                        builder.Append(" Attachment");
                    }
                    else
                    {
                        builder.Append(" Attachments"); 
                    }
                    
                    foreach (var attachment in attachments)
                    {
                        builder.Append("\n\nFile Name: ");
                        builder.Append(attachment.Name);
                        builder.Append(" (");
                        builder.Append(attachment.Size);
                        builder.Append(")\n");
                        builder.Append(attachment.URL);
                        builder.Append("\n");
                    }
                }
                return builder.ToString();
            }
        }

        public class Attachment
        {
            public string Name { get; set; }
            public string Size { get; set; }
            public string URL { get; set; }
        }
    }

}